package goldeneagle.com.admin.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import goldeneagle.com.admin.dao.AdminManagerDao;
import goldeneagle.com.entity.Product;
import goldeneagle.com.entity.User;
import goldeneagle.com.util.HibernateUtil;

public class AdminManagerDaoImpl implements AdminManagerDao {
	// 用户查询分页实现
	@Override
	public List<User> adminSelectUserList(int currentPage, int size) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			Query<User> q = session.createQuery("select new User(id,username,password,address,email,img,userType) from User");
			q.setFirstResult((currentPage - 1) * size);
			q.setMaxResults(size);
			List<User> list = q.list();
			tran.commit();
			return list;
		} catch (Exception e) {
			tran.rollback();
		}
		return null;
	}

	// 管理员根据id删除用户
	@Override
	public boolean adminDeleteUser(int id) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			User user = session.load(User.class, id);
			session.delete(user);
			tran.commit();
			return true;
		} catch (Exception e) {
			tran.rollback();
		}
		return false;
	}

	// 管理员添加新用户or管理员
	@Override
	public boolean adminAddUser(User user) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			session.save(user);
			tran.commit();
			return true;
		} catch (Exception e) {
			tran.rollback();
			e.printStackTrace();
		}

		return false;
	}

	// id 单查
	@Override
	public User findUserById(int id) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			User user = session.load(User.class, id);
			User u = new User(user.getId(), user.getUsername(), user.getPassword(), user.getAddress(), user.getEmail(),
					user.getImg(), user.getUserType());
			tran.commit();
			return u;
		} catch (Exception e) {
			tran.rollback();
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean adminUpdateUser(User user) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			User u = session.load(User.class, user.getId());
			u.setUsername(user.getUsername());
			u.setPassword(user.getPassword());
			u.setAddress(user.getAddress());
			u.setEmail(user.getEmail());
			u.setUserType(user.getUserType());
			session.update(u);
			tran.commit();
			return true;
		} catch (Exception e) {
			tran.rollback();
			e.printStackTrace();
		}
		return false;
	}

	// =====================================
	@Override
	public List adminSelectProductList(int currentPage, int size) {
		Session session = null;
		Transaction tran = null;
		List<Product> productList = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			Query query = session.createQuery("select new Product (id,name,price,description,img,ptype) from Product");
			query.setFirstResult((currentPage - 1) * size);
			query.setMaxResults(size);
			productList = query.list();
			productList.forEach(action -> System.out.println(action.getName()));
			tran.commit();

		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			assert session != null;
			session.close();

		}
		return productList;
	}

	@Override
	public boolean adminDeleteProduct(Product product) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			session.delete(product);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			session.close();
		}
		return false;
	}

	@Override
	public boolean adminAddProduct(Product product) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			session.save(product);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public boolean adminUpdateProduct(Product product) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			Product p = session.load(Product.class, product.getId());
			p.setName(product.getName());
			p.setPrice(product.getPrice());
			p.setDescription(product.getDescription());
			p.setImg(product.getImg());
			p.setPtype(product.getPtype());
			// session.update(product);
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			session.close();
		}
		return true;
	}

	@Override
	public Product findProductById(int id) {
		Session session = null;
		Transaction tran = null;
		Product product = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			Product p = session.load(Product.class, id);
			product = new Product(p.getId(), p.getName(), p.getPrice(), p.getDescription(), p.getImg(), p.getPtype());
			tran.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tran.rollback();
		} finally {
			session.close();
		}
		return product;
	}

	//
	@Override
	public long totalCount(String name) {
		Session session = null;
		Transaction tran = null;
		try {
			session = HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			Query q = session.createQuery("select count(*) from " + name);
			Long count = (Long) q.uniqueResult();
			tran.rollback();
			return count;
		} catch (Exception e) {
			tran.rollback();
		}
		return 0;
	}

	public static void main(String[] args) {
		User user = new User();
		AdminManagerDaoImpl ad = new AdminManagerDaoImpl();
		user.setId(1);
		user.setUsername("hjx");
		user.setPassword("1234");
		user.setAddress("上下上下左右左右");
		user.setEmail("48454@qq.com");
		user.setUserType("admin");
		user.setImg("pdx.gif");
		System.out.println(ad.adminUpdateUser(user));
//		System.out.println(ad.adminAddUser(user));
//		System.out.println(ad.totalCount("User"));
//		List<User> list = ad.adminPageSelectUserList(1,10);
//		list.forEach(action->System.out.println(action.getUsername()));
	}

}
